import QtQuick
import QtQuick.Controls
import QtQuick.Particles

pragma ComponentBehavior: Bound

Item {
    id: root
    width: 480
    height: 320

    property var system: ParticleSystem {
        id: particleSystem
    }

    property int emitRate: 10              // 生成速率
    property int lifeSpan: 4000            // 寿命
    property int lifeSpanVariation: 2000    // 寿命随机
    property int size: 32                  // 生成粒子大小
    property int endSize: 64               // 粒子最终大小

    // 粒子表
    property var itemList: []
    Component {
        id: particleDelegateModel

        RandomItemParticle {
            // itemList: root.itemList
        }
    }

    // 速度
    property real magnitude: 50           // 速度
    property real magnitudeVariation: 20   // 速度随机
    property real angle: 0                 // 发射方向
    property real angleVariation: 180      // 方向随机

    // 限定粒子生成位置
    property var center: Qt.vector2d(width / 2, height / 2)
    property var recSize: Qt.vector2d(width, height)

    property real topMargin: center.y - recSize.y / 2
    property real leftMargin: center.x - recSize.x / 2

    // 影响器
    property bool mouseAttract: true        // 鼠标吸引
    property real mouseAttractStrength: 0.3
    property bool mouseAttractOnce: false

    Emitter {
        id: emitter
        width: root.recSize.x
        height: root.recSize.y

        anchors.top: root.top
        anchors.left: root.left
        anchors.topMargin: root.topMargin
        anchors.leftMargin: root.leftMargin

        system: particleSystem

        emitRate: root.emitRate
        lifeSpan: root.lifeSpan
        lifeSpanVariation: root.lifeSpan
        size: root.size
        endSize: root.endSize

        velocity: AngleDirection {
            angle: root.angle
            angleVariation: root.angleVariation
            magnitude: root.magnitude
            magnitudeVariation: root.magnitudeVariation
        }
    }

    Loader {
        id: mouseAttractorLoader
        anchors.fill: parent
    }

    ItemParticle {
        system: particleSystem
        delegate: particleDelegateModel
    }

    // 鼠标影响器
    Component {
        id: mouseAttractorComponent

        Attractor {
            id: mouseAttractor
            anchors.fill: parent

            pointX: width / 2
            pointY: height / 2

            strength: root.mouseAttractStrength
            system: particleSystem
            once: root.mouseAttractOnce

            MouseArea {
                anchors.fill: parent

                propagateComposedEvents: true

                HoverHandler {
                    onHoveredChanged: {
                        updateAttractCenter()
                    }

                    onPointChanged: {
                        updateAttractCenter()
                    }

                    Component.onCompleted: {
                        updateAttractCenter()
                    }

                    function updateAttractCenter(){
                        if (!hovered) {
                            mouseAttractor.enabled = false
                        }
                        else {
                            mouseAttractor.enabled = true
                            mouseAttractor.pointX = point.position.x
                            mouseAttractor.pointY = point.position.y
                        }
                    }
                }
            }
        }
    }

    Component.onCompleted: {
        if (root.mouseAttract) {
            mouseAttractorLoader.sourceComponent = mouseAttractorComponent
        }
    }
}
